/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.flexengine.logger;

import java.io.PrintStream;
import java.util.Locale;
import javax.swing.JTextArea;

/**
 * @author Luann R. Athayde
 * @version 1.0
 * @since 1.0
 */
public class JTextAreaPrintStream extends PrintStream {
    
    private JTextArea textArea;
    private String    temp;
    
    public JTextAreaPrintStream(JTextArea textArea) {
        super(System.out);
        this.textArea = textArea;
        this.temp     = null;
        this.textArea.setText("");
    }

    @Override
    public PrintStream append(CharSequence csq) {
        temp = textArea.getText() + csq;
        textArea.setText(temp);
        return this;
    }

    @Override
    public PrintStream append(CharSequence csq, int start, int end) {
        temp = textArea.getText() + csq.subSequence(start, end);
        textArea.setText(temp);
        return this;
    }

    @Override
    public PrintStream append(char c) {
        temp = textArea.getText() + c;
        textArea.setText(temp);
        return this;
    }

    @Override
    public boolean checkError() {
        return false;
    }

    @Override
    protected void clearError() {
    }

    @Override
    public void close() {
        textArea = null;
    }

    @Override
    public void flush() {
    }

    @Override
    public PrintStream format(String format, Object ... args) {
        temp = textArea.getText();
        for(int i=0;i<args.length;i++) {
            format = format.replace("{"+(i+1)+"}", args[i].toString());
        }
        temp += format;
        textArea.setText(temp);
        return this;
    }

    @Override
    public PrintStream format(Locale l, String format, Object ... args) {
        temp = textArea.getText();
        for(int i=0;i<args.length;i++) {
            format = format.replace("{"+(i+1)+"}", args[i].toString());
        }
        temp += format;
        textArea.setText(temp);
        return this;
    }

    @Override
    public void print(boolean b) {
        temp = textArea.getText()+b;
        textArea.setText(temp);
    }

    @Override
    public void print(char c) {
        temp = textArea.getText()+c;
        textArea.setText(temp);
    }

    @Override
    public void print(int i) {
        temp = textArea.getText()+i;
        textArea.setText(temp);
    }

    @Override
    public void print(long l) {
        temp = textArea.getText()+l;
        textArea.setText(temp);
    }

    @Override
    public void print(float f) {
        temp = textArea.getText()+f;
        textArea.setText(temp);
    }

    @Override
    public void print(double d) {
        temp = textArea.getText()+d;
        textArea.setText(temp);
    }

    @Override
    public void print(char[] s) {
        temp = textArea.getText()+new String(s);
        textArea.setText(temp);
    }

    @Override
    public void print(String s) {
        temp = textArea.getText()+s;
        textArea.setText(temp);
    }

    @Override
    public void print(Object obj) {
        temp = textArea.getText()+obj;
        textArea.setText(temp);
    }

    @Override
    public PrintStream printf(String format, Object... args) {
        return format(format, args);
    }

    @Override
    public PrintStream printf(Locale l, String format, Object... args) {
        return format(format, args);
    }

    @Override
    public void println() {
        temp = textArea.getText()+"\n";
        textArea.setText(temp);
    }

    @Override
    public void println(boolean x) {
        temp = textArea.getText()+x+"\n";
        textArea.setText(temp);
    }

    @Override
    public void println(char x) {
        temp = textArea.getText()+x+"\n";
        textArea.setText(temp);
    }

    @Override
    public void println(int x) {
        temp = textArea.getText()+x+"\n";
        textArea.setText(temp);
    }

    @Override
    public void println(long x) {
        temp = textArea.getText()+x+"\n";
        textArea.setText(temp);
    }

    @Override
    public void println(float x) {
        temp = textArea.getText()+x+"\n";
        textArea.setText(temp);
    }

    @Override
    public void println(double x) {
        temp = textArea.getText()+x+"\n";
        textArea.setText(temp);
    }

    @Override
    public void println(char[] x) {
        temp = textArea.getText()+new String(x)+"\n";
        textArea.setText(temp);
    }

    @Override
    public void println(String x) {
        temp = textArea.getText()+x+"\n";
        textArea.setText(temp);
    }

    @Override
    public void println(Object x) {
        temp = textArea.getText()+x+"\n";
        textArea.setText(temp);
    }

    @Override
    protected void setError() {
    }

    @Override
    public void write(int b) {
        temp = textArea.getText()+b;
        textArea.setText(temp);
    }

    @Override
    public void write(byte[] buf, int off, int len) {
        temp = textArea.getText()+new String(buf).substring(off,len);
        textArea.setText(temp);
    }
}
